﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using KinectGestureRecognizer;

namespace testKinect
{
    public class FrameRateCounter : DrawableGameComponent
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        int frameRate = 0;
        int numberOfKinectEventsPerSecond = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;

        kinectController kinect;

        public FrameRateCounter(Game game, kinectController kinect)
            : base(game)
        {
            content = new ContentManager(game.Services);
            this.kinect = kinect;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = content.Load<SpriteFont>("Content/Font");
        }

        protected override void UnloadContent()
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                numberOfKinectEventsPerSecond = kinect.numberOfKinectEvents;
                kinect.numberOfKinectEvents = 0;
                frameCounter = 0;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            frameCounter++;

            spriteBatch.Begin();

            spriteBatch.DrawString(spriteFont, string.Format("fps: {0}", frameRate), new Vector2(5, 5), Color.White);
            spriteBatch.DrawString(spriteFont, string.Format("kinectEventPerSecond: {0}", numberOfKinectEventsPerSecond), new Vector2(5, 15), Color.White);

            spriteBatch.End();
        }
    }
}
